import pathlib
import time
import re
import typing
import os
import yaml
import ctypes
import psutil
import subprocess
import requests
import winsound
import datetime
import rich
from selenium.webdriver.edge.webdriver import WebDriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import *









def _Find_input_element(driver:WebDriver,label:str) -> WebElement:
    anchor_element = driver.find_elements(by=By.XPATH,value=f"//*[(self::span or self::label or self::div) and text()='{label}']")
    if len(anchor_element) == 1:
        find_xpath_path = "./parent::*//input"
        while True:
            try:
                element = anchor_element[0].find_element(by=By.XPATH,value=find_xpath_path)
                return element
            except NoSuchElementException:
                print(f"提示 : 当前层级没有找到 Input 元素,继续向上查找!")
                find_xpath_path = find_xpath_path.replace("//input","/parent::*//input")
                continue



def _Find_ByType_input_element(driver:WebDriver,label:str,InputType:typing.Literal["","text","file"]="text",InputAccept:typing.Literal['',"video","mp4","image"]="",Multiple:bool=False,max_attempts:int = 5) -> WebElement:
    """用于快速的查找定位 Input 元素.

    Args:
        driver (WebDriver): WebDriver 对象
        label (str): 锚点文本
        InputType (typing.Literal[&quot;&quot;,&quot;text&quot;,&quot;file&quot;], optional): Input 元素的类型. Defaults to "text".
        InputAccept (typing.Literal[&#39;&#39;,&quot;video&quot;,&quot;mp4&quot;,&quot;image&quot;], optional): Input 元素的属性. Defaults to "".
        Multiple (bool, optional): 是否支持多选. Defaults to False.
        max_attempts (int, optional): 最大查找次数. Defaults to 5.

    Returns:
        WebElement: Input 元素.
    """
    # 锚点元素必须是全局唯一的
    print(f"//*[(self::span or self::label or self::div) and text()='{label}']")
    elements = driver.find_elements(by=By.XPATH,value=f"//*[(self::span or self::label or self::div) and text()='{label}']")

    if elements.__len__() == 1:
        anchor_element = elements[0]
        find_xpath_path = f"./parent::*"
        input_xpath = "input"
        
        if InputType != "":
            input_xpath += f"[@type='{InputType}']"
        if InputAccept != "":
            input_xpath += f"[contains(@accept,'{InputAccept}')]"
        if Multiple == True:
            input_xpath += f"[@multiple]"
        
        find_xpath_path = f"./parent::*//{input_xpath}"

        attempt = 0
        while True:
            try:
                print(f"提示 : ./parent::*//{input_xpath}")
                element = anchor_element.find_element(by=By.XPATH,value=find_xpath_path)
                return element
            except NoSuchElementException:
                # print(f"提示 : 当前层级没有找到 Input 元素,继续向上查找!")
                # find_xpath_path = find_xpath_path.replace(f"//{input_xpath}",f"/parent::*//{input_xpath}")
                attempt += 1
                anchor_element = anchor_element.find_element(by=By.XPATH,value="./parent::*")
                
                if attempt >= max_attempts:
                    print(f"提示 : 尝试次数已达到最大值,退出查找!")
                    break
                else:
                    continue
    else:
        print(f"提示 : 锚点元素必须是全局唯一的!当前锚点元素个数 : {elements.__len__()}")